using System;
using System.Web;
using System.Configuration;
using DemoPress.BlogServices.Configuration;

namespace DemoPress.BlogServices
{
    public class DemoPressDataModule: IHttpModule
    {
        private const string CONTEXTKEY = "DemoPressBlogService";

        #region IHttpModule Members

        public void Dispose()
        {
        }

        public void Init(HttpApplication context)
        {
            context.BeginRequest += new EventHandler(context_BeginRequest);
        }

        void context_BeginRequest(object sender, EventArgs e)
        {
            DemoPressDataSectionHandler handler = (DemoPressDataSectionHandler) ConfigurationManager.GetSection("modulesSection/demoPresDataModule");
            SetBlogService(BlogService.CreateService(handler.Assembly, handler.DataService));
        }

        #endregion

        private void SetBlogService(BlogService service)
        {
            HttpContext.Current.Items.Add(CONTEXTKEY, service);
        }
    }
}
